package com.hb.trade.api.routine.service;

import com.hb.core.pojo.enums.FeeTypeEnum;
import com.hb.core.pojo.project.file.entity.BidBuyRecord;
import com.hb.core.pojo.project.order.dto.UserOrderDTO;
import com.hb.core.pojo.project.order.entity.UserOrder;
import com.hb.core.pojo.project.order.entity.UserOrderGoods;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.trade.client.center.OrgClient;
import com.hb.trade.client.project.BidFileInfoClient;
import com.hb.trade.client.project.SectionClient;
import com.hb.trade.client.project.UserOrderClient;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.Enums;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 支付相关逻辑
 *
 * @author zhaojk
 */
@Slf4j
@Service
public class PayService {

    protected final UserOrderClient userOrderClient;
    protected final BidFileInfoClient bidFileInfoClient;
    protected final SectionClient sectionClient;
    protected final OrgClient orgClient;

    public PayService(UserOrderClient userOrderClient,
                      BidFileInfoClient bidFileInfoClient,
                      SectionClient sectionClient,
                      OrgClient orgClient) {
        this.userOrderClient = userOrderClient;
        this.bidFileInfoClient = bidFileInfoClient;
        this.sectionClient = sectionClient;
        this.orgClient = orgClient;
    }

    /**
     * 处理订单支付成功之后的逻辑
     *
     * @return
     */
    public boolean paySuccessOperations(UserOrder userOrder) {
        // 查询订单信息
        Message<UserOrderDTO> detailMsg = userOrderClient.orderDetail(userOrder.to(UserOrderDTO.class));
        if (detailMsg.isFail()) {
            log.warn(detailMsg.getMessage());
            return false;
        }
        UserOrderDTO data = detailMsg.getData();
        List<UserOrderGoods> dataGoods = data.getGoods();
        // 根据商品信息执行业务操作
        for (UserOrderGoods dataGood : dataGoods) {
            Integer goodFeeType = dataGood.getFeeType();
            FeeTypeEnum feeTypeEnum = Enums.of(FeeTypeEnum.class, goodFeeType);
            if (feeTypeEnum == null) {
                return false;
            }
            switch (feeTypeEnum) {
                case DOC_BOOK:
                    // 增加一条购标记录
                    BidBuyRecord buyRecord = new BidBuyRecord();
                    buyRecord.setBuyUserId(userOrder.getUserId());

                    buyRecord.setOrgId(dataGood.getOrgId());
                    buyRecord.setSectionId(dataGood.getSectionId());

                    buyRecord.setOrderNo(data.getOutOrderNo());
                    buyRecord.setDownTime(LocalDateTime.now());
                    buyRecord.buildBaseData(1);
                    bidFileInfoClient.saveRecord(buyRecord);

                    // 标书费支付成功后将投标人信息保存至供应商库
                    BidSectionDTO bidSection = new BidSectionDTO();
                    bidSection.setSectionId(dataGood.getSectionId());
                    Message<BidSectionDTO> bidSectionProjectInfo = sectionClient.getBidSectionProjectInfo(bidSection);
                    if (bidSectionProjectInfo.isSuccess() && null != bidSectionProjectInfo.getData()) {
                        orgClient.tenderSaveSupply(bidSectionProjectInfo.getData().getOrgId(), ShiroUtil.getOrgId());
                    }

                    break;
                case BID_PRO:
                case PER_PRO:
                case SIGN_UP:
                case SYS_USE:
                case OTHER:
                default:
                    break;
            }

        }
        return true;
    }
}
